package net.lab1024.sa.admin.module.business.erp.mmo.service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.lab1024.sa.admin.module.business.erp.bas.dao.BatchordDao;
import net.lab1024.sa.admin.module.business.erp.mmo.dao.PrfordDao;
import net.lab1024.sa.admin.module.business.erp.mmo.dao.PrfordaDao;
import net.lab1024.sa.admin.module.business.erp.bas.domain.entity.BatchordEntity;
import net.lab1024.sa.admin.module.business.erp.mmo.dao.PwrordaDao;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.entity.MrqordaEntity;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.entity.PrfordEntity;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.entity.PrfordaEntity;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.entity.PwrordaEntity;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.form.PrfordAddForm;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.form.PrfordQueryForm;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.form.PrfordUpdateForm;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.vo.PrfordVO;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.vo.PrfordaVO;
import net.lab1024.sa.base.common.code.ErpErrorCode;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 产品送检单 Service
 *
 * @Author AKITA
 * @Date 2025-08-18 21:57:37
 * @Copyright AKITA
 */

@Service
public class PrfordService {

    @Resource
    private PrfordDao prfordDao;
    @Resource
    private PrfordaDao prfordaDao;
    @Resource
    private BatchordDao batchordDao;
    @Autowired
    private PwrordaDao pwrordaDao;

    /**
     * 分页查询
     */
    public PageResult<PrfordVO> queryPage(PrfordQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<PrfordVO> list = prfordDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }


    @Transactional
    public ResponseDTO<Long> add(PrfordAddForm addForm) {
        PrfordEntity prfordEntity = SmartBeanUtil.copy(addForm, PrfordEntity.class);
        prfordDao.insert(prfordEntity);
        DateTimeFormatter yyyy = DateTimeFormatter.ofPattern("yyyy");
        DateTimeFormatter mm = DateTimeFormatter.ofPattern("MM");
        DateTimeFormatter dd = DateTimeFormatter.ofPattern("dd");
        String year = yyyy.format(LocalDate.now());
        String month = mm.format(LocalDate.now());
        String day = dd.format(LocalDate.now());
        BatchordEntity batchordEntity = new BatchordEntity();
        for (PrfordaEntity prfordaEntity : addForm.getLista()) {
            Long batchNum =  batchordDao.selectMaxId(year,month,day);
            String l = String.valueOf(year + month + day + (batchNum));
            prfordaEntity.setBatchId(l);
            batchordEntity.setBatchId(l);
            batchordEntity.setBatchNum(batchNum);
            batchordEntity.setYear(year);
            batchordEntity.setMonth(month);
            batchordEntity.setDay(day);
            batchordEntity.setId(null);
            batchordDao.insert(batchordEntity);
            prfordaEntity.setId(prfordEntity.getId());
            prfordaDao.insert(prfordaEntity);
        }


        return ResponseDTO.ok(prfordEntity.getId());
    }

    /**
     * 更新
     *
     */
    public ResponseDTO<String> update(PrfordUpdateForm updateForm) {
        PrfordEntity prfordEntity = SmartBeanUtil.copy(updateForm, PrfordEntity.class);
        prfordDao.updateById(prfordEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        prfordDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    @Transactional
    public ResponseDTO<String> delete(Long id) {

        List<PwrordaEntity> entitiesa = pwrordaDao.selectList(new LambdaQueryWrapper<PwrordaEntity>().eq(PwrordaEntity::getBaseId, id));

        if (!entitiesa.isEmpty()){
            return ResponseDTO.error(ErpErrorCode.PARAM_ERROR,"存在产品入库单，禁止删除！");
        }


        prfordDao.deleteById(id);
        prfordaDao.deleteById(id);
        return ResponseDTO.ok();
    }

    public ResponseDTO<PrfordVO> info(Long id) {
        PrfordEntity prfordEntity = prfordDao.selectById(id);
        PrfordVO copy = SmartBeanUtil.copy(prfordEntity, PrfordVO.class);
        List<PrfordaEntity> prfordaEntities = prfordaDao.selectList(new LambdaQueryWrapper<PrfordaEntity>().eq(PrfordaEntity::getId, id));
        List<PrfordaVO> prfordaVOS = SmartBeanUtil.copyList(prfordaEntities, PrfordaVO.class);
        copy.setLista(prfordaVOS);
        return ResponseDTO.ok(copy);
    }

    public ResponseDTO<PrfordVO> infoUnexecute(Long id) {
        List<PrfordVO> poordVOS = prfordDao.infoUnexecute(id);
        return ResponseDTO.ok(poordVOS.get(0));
    }
}
